package com.globagames.strategicgames.view.battles.actions
{
	import com.globagames.isometric.core.event.IsoMapMouseEvent;
	import com.globagames.isometric.core.model.SimpleObject;
	import com.globagames.isometric.core.view.IsoMap;
	import com.globagames.strategicgames.model.Soldier;
	import com.globagames.strategicgames.model.StrategicGameModelLocator;
	import com.globagames.strategicgames.util.SoldierDesigner;
	
	import flash.events.MouseEvent;
	
	import mx.core.UIComponent;
	
	import org.cove.ape.APEngine;
	import org.cove.ape.AbstractParticle;
	import org.cove.ape.CircleParticle;
	import org.cove.ape.RectangleParticle;
	import org.cove.ape.SpringConstraint;
			
	public class MoveActionOld extends UIComponent implements IAction {
		private var model:StrategicGameModelLocator = StrategicGameModelLocator.getInstance();
		var soldierDesigner:SoldierDesigner = new SoldierDesigner();	
		private var _soldier:Soldier;
		private var isomap:IsoMap;
		
		private var movingSoldier:Soldier;
		private var wc:SpringConstraint
		private var oldX:int;
		private var oldY:int;
		public function MoveActionOld() {			
		}
		override protected function createChildren():void{
			super.createChildren();
			
			this.width = model.screenWidth;
			this.height = model.screenHeight;
			
					
		}
		
		public function mouseDownHandler(event:MouseEvent):void {
			this.systemManager.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler,true);
			this.systemManager.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler,false);
			trace("MoveAction::mouseDownHandler");	
			oldX = soldier.soldierIso.ox;
			oldY = soldier.soldierIso.oy;
			isomap.draggable = false;
			movingSoldier = _soldier.clone();
			movingSoldier.soldierIso = new SimpleObject();
			movingSoldier.soldierIso.bitmapData = _soldier.soldierIso.bitmapData.clone();
			movingSoldier.soldierIso.particle =new CircleParticle(_soldier.soldierIso.ox, _soldier.soldierIso.oy, 30);
			isomap.apeDefaultGroup.addParticle(movingSoldier.soldierIso.particle);
			movingSoldier.soldierIso.particle.fixed = false;
			soldier.soldierIso.particle.fixed = false;
			isomap.addIsoObject(movingSoldier.soldierIso,_soldier.soldierId +".clone");
			soldier.soldierIso.bitmapData = soldierDesigner.grayscale(soldier.soldierIso.bitmapData);
			//wc = new SpringConstraint(soldier.soldierIso.particle, movingSoldier.soldierIso.particle, 0.5, true, 120);
            //isomap.apeDefaultGroup.addConstraint(wc);
            
			isomap.invalidateProperties();
			this.systemManager.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);			
		}
		public function mouseUpHandler(event:MouseEvent):void {
			trace("MoveAction::mouseUpHandler");	
			this.systemManager.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);	
			this.systemManager.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler, true);
			this.systemManager.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler, false);
			isomap.draggable = true;	
			isomap.apeDefaultGroup.removeParticle(movingSoldier.soldierIso.particle);
			soldier.soldierIso.particle.px = movingSoldier.soldierIso.particle.px;
			soldier.soldierIso.particle.py = movingSoldier.soldierIso.particle.py;
			isomap.moveIsoObject(soldier.soldierIso, soldier.soldierIso.particle.px,soldier.soldierIso.particle.py);
			isomap.remove(movingSoldier.soldierIso);
			soldier.soldierIso.bitmapData = soldierDesigner.getActionSelectedNeutralBitmap(soldier.isCaptain,model.armyModule.getBitmapForSoldierCode(soldier.profileCode));
			isomap.doRetrace = true;
			isomap.invalidateProperties();
		}
		public function mouseMoveHandler(event:MouseEvent):void {
			trace("MoveAction::mouseMoveHandler");
			var particle:AbstractParticle = movingSoldier.soldierIso.particle;
			var position:IsoMapMouseEvent = isomap.getCurrentMousePosition(mouseX,mouseY);
        		particle.px = position.point2d.x;
	        	particle.py = position.point2d.y;
	        	
	        	APEngine.step();			     
			    if( ! particle.hasCollided ) {
			    	
			        movingSoldier.soldierIso.ox = particle.px;
			        movingSoldier.soldierIso.oy = particle.py;
			        isomap.moveIsoObject(movingSoldier.soldierIso, particle.px,particle.py);
			        isomap.doRetrace = true;
					isomap.invalidateProperties();
			    }      	            
			    
		}
		public function attach(soldier:Soldier, isomap:IsoMap):void {
			this._soldier = soldier;
			this.isomap = isomap;
			this.systemManager.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
		}
		public function detach():void {
			this.systemManager.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			this._soldier = null;
			this.isomap = null;
		}
		
		public function get soldier ():Soldier {
			return _soldier;
		}
		

	}
}